/**/
#include "global.h"
#include "prismaplus_dm6437.h"

void GPIO_IntInit();

void Interrupt_init()
{
	CSR=0x100; 	 /* disable all interrupts			  */
	IER=1; 		 /* disable all interrupts except NMI */
	ICR=0xffff;	 /* clear all pending interrupts	  */   

	ISTP = 0x10800000; //  
	 
	INTC_EVTCLR0 = 0xFFFFFFFF; 
	INTC_EVTCLR1 = 0xFFFFFFFF; 
	INTC_EVTCLR2 = 0xFFFFFFFF; 
	INTC_EVTCLR3 =	0xFFFFFFFF;   
	//INTC_EVTFLAG1=0x00000000;
	INTC_EVTMASK3 = 0xFFFFFFFF;
	INTC_EVTMASK2 = 0xFFFFFFFF;
	INTC_EVTMASK1 = 0xFFFFFFFF;
	INTC_EVTMASK0 = 0xFFFFFFFF;

	INTC_INTMUX1 = 0x00000000;
	INTC_INTMUX2 = 0x04201E00; //   VPSS-VPBE (VENC) -> Int10   Rsz -> Int9 timer0->11
	INTC_INTMUX3 = 0x004D4C00; //   GPIOBNK5 -> Int14 GPIOBNK4-> Int13
	
	IER |= 0x00006E02;
	CSR=0x01 ;		 /* enable all interrupts			 */

	GPIO_IntInit();
		
}

void GPIO_IntInit()
{
	GPIO_BINTEN = 0x00000030;
	//GPIO_DIR45 = 0x0004C400;
	GPIO_DIR45 = 0x00000000;
	
	GPIO_CLR_RIS_TRIG45 = 0x0004FFFF;
	GPIO_SET_RIS_TRIG45 = 0x0004C400;
	//GPIO_SET_FAL_TRIG45 = 0x0004C400;
	
}

void Timer_IntInit()
{
	TIMER0_TGCR = 0;
	TIMER0_EMUMGT = 0x0003;
	TIMER0_TIM12 = 0;
	TIMER0_TIM34 = 0;
	//TIMER0_PRD12 = 0x00041EB0; //0xFFFFFFFF; //10ms
	//TIMER0_PRD34 = 0x0; //0xFFFFFFFF;
	TIMER0_PRD12 = 0x00841EB0; //0xFFFFFFFF; //10ms
	TIMER0_PRD34 = 0x0; //0xFFFFFFFF;
	TIMER0_TRC = 0
        | ( 1 << 22 )                           // Enable34      [Continuous]
        | ( 0 << 8 )                            // Clock source? [TIMERIN]
        | ( 1 << 6 );                           // Enable12      [Continuous]
    TIMER0_TGCR = 0
        | ( 0 << 12 )                           // TDDR34
        | ( 0 << 8 )                            // PSC34
        | ( 0 << 2 )                            // TIMMODE
        | ( 1 << 1 )                            // TIM34RS [Enable]
        | ( 1 << 0 );                           // TIM12RS [Enable]

    CFG_TIMERCTL |= ( 1 << 1 );                 // Timer 0 external input w/o divide
}

void StartTimer0()
{
	TIMER0_TGCR = 0x03;
}

void StopTimer0()
{
	TIMER0_TGCR = 0x0;
	TIMER0_TIM12 = 0x0;
	TIMER0_TIM34 = 0x0;
}

interrupt void Int_RszISR(void)  
{
	RszDoneBufAddr = VpfeInBufAddr[inbufcnt];
	inbufcnt++;
    inbufcnt =	inbufcnt&0x03;
	VPFE_RESZ_SDR_OUTADD    = VpfeInBufAddr[inbufcnt];
	RszDoingBufAddr = VpfeInBufAddr[inbufcnt];
	if(resize_flag)
	{
		switch(g_mode_resize)
		{
			case MODE_RSZ_X1:
				VPFE_RESZ_RSZ_CNT       = 0x1003F4FD;
				VPFE_RESZ_IN_START      = 0x00000000;
				VPFE_RESZ_SDR_INADD		= CCDCINADDR;
				VPFE_RESZ_IN_SIZE       = 0x01E00280;
				break;
			case MODE_RSZ_X2:
				VPFE_RESZ_RSZ_CNT       = 0x100374DD;
				VPFE_RESZ_IN_START      = 0x00000000; //0x001E0028;
				VPFE_RESZ_SDR_INADD		= (CCDCINADDR+1280*30+40*2);
				VPFE_RESZ_IN_SIZE       = 0x01A40230;
				break;
			case MODE_RSZ_X4:
				VPFE_RESZ_RSZ_CNT       = 0x1002F4BD;
				VPFE_RESZ_IN_START      = 0x00000000; //0x003C0050;
				VPFE_RESZ_SDR_INADD		= (CCDCINADDR+1280*60+80*2);
				VPFE_RESZ_IN_SIZE       = 0x016801E0;
				break;
			case MODE_RSZ_X8:
				VPFE_RESZ_RSZ_CNT       = 0x1002749D;
				VPFE_RESZ_IN_START      = 0x00000000; //0x005A0078;
				VPFE_RESZ_SDR_INADD		= (CCDCINADDR+1280*90+120*2);
				VPFE_RESZ_IN_SIZE       = 0x012C0190;
				break;
			case MODE_RSZ_X16:
				VPFE_RESZ_RSZ_CNT       = 0x1001F47D;
				VPFE_RESZ_IN_START      = 0x00000000; //0x007800A0;
				VPFE_RESZ_SDR_INADD		= (CCDCINADDR+1280*120+160*2);
				VPFE_RESZ_IN_SIZE       = 0x00f00140;
				break;
			default:
				break;
		}
		resize_flag = 0;
	}
	
	debug0++;
	INTC_EVTCLR0            = 0x40000000;
	VPFE_RESZ_PCR           = 0x00000001;
	
	return;
}


interrupt void Int_VencISR(void)
{
	VPBE_OSD_VIDWIN0ADR	= VencDoingBufAddr;
	debug1++;
	INTC_EVTCLR1            = 0x00000001;

	return;

}

/*
 GPIO74   KEY_MODE
 GPIO78   KEY_SUB
 GPIO79   KEY_ADD
 GPIO82   KEY_FREEZE
 */

interrupt void Int_GPIOBNK4ISR(void)
{
	debug2++;
	
	if((GPIO_INSTAT45&0x00000400)&&(key_flag != FGPIO74))// MODE
	{
		key_flag = FGPIO74;
		StartTimer0();
		GPIO_INSTAT45 = 0x00000400;
		if(g_mode==MODE_RESIZE)
		{
			g_mode = MODE_COLOUR;
		}
		else
		{
			g_mode = MODE_RESIZE;
		}
	}
	else if((GPIO_INSTAT45&0x00004000)&&(key_flag != FGPIO78))// SUB
	{
		key_flag = FGPIO78;
		StartTimer0();
		GPIO_INSTAT45 = 0x00004000;
		if(g_mode==MODE_RESIZE)
		{
			resize_flag = 1;
			g_mode_resize--;
			if(g_mode_resize<0)
				g_mode_resize = 0;
		}
		else
		{
			g_mode_color = (g_mode_color + MODE_CLR_NUM - 1)%MODE_CLR_NUM;
		}
	}
	else if((GPIO_INSTAT45&0x00008000)&&(key_flag != FGPIO79))// ADD
	{
		GPIO_INSTAT45 = 0x00008000;
		key_flag = FGPIO79;
		StartTimer0();
		if(g_mode==MODE_RESIZE)
		{
			resize_flag = 1;
			g_mode_resize++;
			if(g_mode_resize>MODE_RSZ_NUM)
				g_mode_resize = MODE_RSZ_NUM;
		}
		else
		{
			g_mode_color = (g_mode_color + 1)%MODE_CLR_NUM;
		}
	}
	else
	{
		debug4++;
		GPIO_INSTAT45 = 0x0000FFFF;
		//key_flag = 0;
	}

	INTC_EVTCLR2            = 0x00001000;
	return;
}

interrupt void Int_GPIOBNK5ISR(void)
{
	debug3++;
	if((GPIO_INSTAT45&0x00040000)&&(key_flag != FGPIO82))// FREEZE
	{
		key_flag = FGPIO82;
		StartTimer0();
		GPIO_INSTAT45 = 0x00040000;
		if(g_freeze==0)
		{
			g_freeze = 1;
		}
		else
		{
			g_freeze = 0;
		}
	}
	else
	{
		debug5++;
		GPIO_INSTAT45 = 0xFFFF0000;
		//key_flag = 0;
	}

	INTC_EVTCLR2            = 0x00002000;
	return;
}

interrupt void Int_TimerISR(void)
{
	StopTimer0();
	key_flag = 0;
	debug6++;
}


